//Documentation complete
/**
 * @author Kazu
 * @version 2009FE09
 */

package problemDomain;

import static problemDomain.ErrorBean.ErrorCode.*;
import static utilities.XMLTools.encodeEntities;

/**
 * Represents an employee account.
 */
public class EmployeeAccountBean implements Bean {
	
	/* Constants */
	
	private static final long serialVersionUID = -9025202057944661258L;
	
	/* END Constants */
	
	/* Attributes */
	
	private int id;
	private String username;
	private String password;
	private String firstname;
	private String lastname;
	private Position position;
	private long passcode;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs an <code>EmployeeAccountBean</code> with default values.
	 */
	public EmployeeAccountBean () {
		id = 0;
		username = "";
		password = "";
		firstname = "";
		lastname = "";
		position = null;
		passcode = 0L;
	}
	
	/**
	 * Constructs an <code>EmployeeAccountBean</code> with an ID.
	 * @param id the ID of the employee account
	 */
	public EmployeeAccountBean (int id) {
		this();
		this.id = id;
	}
	
	/**
	 * Constructs an <code>EmployeeAccountBean</code> with specified values.
	 * @param id the ID of the employee
	 * @param username the username of the employee
	 * @param password the password of the employee
	 * @param firstname the first name of the employee
	 * @param lastname the last name of the employee
	 * @param position the position of the employee
	 * @param passcode the passcode of the employee
	 */
	public EmployeeAccountBean (int id, String username,
		String password, String firstname, String lastname,
		Position position, long passcode) {
		this.id = id;
		this.username = username;
		this.password = password;
		this.firstname = firstname;
		this.lastname = lastname;
		this.position = position;
		this.passcode = passcode;
	}
	
	/* END Constructors */
	
	
	/* Accessors */
	
	/**
	 * Returns the account ID of the <code>EmployeeAccountBean</code>.
	 * @return the account ID of the employee account
	 */
	public int getID () {
		return id;
	}
	
	/**
	 * Returns the user name of the <code>EmployeeAccountBean</code>
	 * @return the user name of the employee account
	 */
	public String getUsername () {
		return username;
	}
	
	/**
	 * Returns the password of the <code>EmployeeAccountBean</code>
	 * @return the password
	 */
	public String getPassword () {
		return password;
	}
	
	/**
	 * Returns the first name of the <code>EmployeeAccountBean</code>
	 * @return the first name
	 */
	public String getFirstname () {
		return firstname;
	}
	
	/**
	 * Returns the last name of the <code>EmployeeAccountBean</code>
	 * @return the last name
	 */
	public String getLastname () {
		return lastname;
	}
	
	/**
	 * Returns passcode of the <code>EmployeeAccountBean</code>.
	 * @return the passcode of the employee
	 */
	public long getPasscode () {
		return passcode;
	}

	/**
	 * Returns the position of the <code>EmployeeAccountBean</code>.
	 * @return the position of the employee
	 */
	public Position getPosition () {
		return position;
	}

	/* END Accessors */
	
	
	/* Mutators */
	
	/**
	 * Sets the account ID of the <code>EmployeeAccountBean</code>.
	 * @param id the new employee account ID
	 */
	public void setID (int id) {
		this.id = id;
	}
	
	/**
	 * Sets the user name of the <code>EmployeeAccountBean</code>.
	 * @param username the new user name
	 */
	public void setUsername (String username) {
		this.username = username;
	}

	/**
	 * Sets the password of the <code>EmployeeAccountBean</code>.
	 * @param password the new password
	 */
	public void setPassword (String password) {
		this.password = password;
	}
	
	/**
	 * Sets the first name of the <code>EmployeeAccountBean</code>.
	 * @param firstname the new first name
	 */
	public void setFirstname (String firstname) {
		this.firstname = firstname;
	}
	
	/**
	 * Sets the last name of the <code>EmployeeAccountBean</code>.
	 * @param lastname the new last name
	 */
	public void setLastname (String lastname) {
		this.lastname = lastname;
	}
	
	/**
	 * Sets the position of the <code>EmployeeAccountBean</code>.
	 * @param position The position to set
	 */
	public void setPosition (Position position) {
		this.position = position;
	}

	/**
	 * Sets the passcode of the <code>EmployeeAccountBean</code>.
	 * @param passcode The passcode to set
	 */
	public void setPasscode (long passcode) {
		this.passcode = passcode;
	}

	/* END Mutators */
	
	
	/* (Bean) Methods */
	
	@Override
	public boolean isValid () throws ErrorBean {
		if (id < 0)
			throw new ErrorBean(
				EMPLOYEE_ID_INVALID,
				String.valueOf(id));
		/*
		if (username != null && username.length() > 15)
			throw new ErrorBean(
				EMPLOYEE_USERNAME_TOO_LONG,
				username);
		
		if (password != null && password.length() > 15)
			throw new ErrorBean(
				EMPLOYEE_PASSWORD_TOO_LONG);
		else if (password != null && password.length() < 6)
			throw new ErrorBean(
				EMPLOYEE_PASSWORD_TOO_SHORT);
		*/
		if (firstname != null && firstname.length() > 30)
			throw new ErrorBean(
				EMPLOYEE_FIRST_NAME_INVALID,
				firstname);
		
		if (lastname != null && lastname.length() > 30)
			throw new ErrorBean(
				EMPLOYEE_LAST_NAME_INVALID,
				lastname);
		
		if (passcode < 0)
			throw new ErrorBean(
				EMPLOYEE_PASS_CODE_INVALID,
				String.valueOf(passcode));
		
		if (position == null)
			throw new ErrorBean(
				EMPLOYEE_PRIVILEGES_NOT_SPECIFIED);
		
		return true;
	}
	
	@Override
	public String toXML () {
		StringBuilder sb = new StringBuilder();
		sb.append("<employee>");
		sb.append("<id>" + id + "</id>");
		sb.append("<firstname>" + firstname + "</firstname>");
		sb.append("<lastname>" + lastname + "</lastname>");
		sb.append("<position>" + (position != null ? position.toString() : "") + "</position>");
		sb.append("<username>" + username + "</username>");
		//sb.append("<password>" + password + "</password>");
		//sb.append("<passcode>" + passcode + "</passcode>");
		sb.append("</employee>");
		return encodeEntities(sb.toString());
	}
	
	/* END (Bean) Methods */
	
	
	/* (Object) Methods */
	
	@Override
	public String toString () {
		StringBuilder sb = new StringBuilder();
		sb.append("ID: " + id + '\n');
		sb.append("First Name: " + firstname + '\n');
		sb.append("Last Name: " + lastname + '\n');
		sb.append("Position: " + (position != null ? position.toString():"") + '\n');
		sb.append("Username: " + username + '\n');
		sb.append("Password: " + password + '\n');
		sb.append("Passcode: " + passcode + '\n');
		return sb.toString();
	}
	
	/* END (Object) Methods */
	
	
	/* (Position) Enumerator */
	
	/**
	 * The account position of an employee.
	 */
	public static enum Position {
		/** Position of manager: Can access all functions. */
		MANAGER(1, "Manager"),
		
		/** Position of host */
		HOST(2, "Host"),
		
		/** Position of server */
		SERVER(3, "Server"),
		
		/** Position of driver */
		DRIVER(4, "Driver"),
		
		/** Position of cook */
		COOK(5, "Cook")
		
		;
		
		/* Attributes */
		
		private int code;
		private String name;
		
		/* END Attributes */
		
		
		/* Constructors */
		
		private Position (int code, String name) {
			this.code = code;
			this.name = name;
		}
		
		/* END Constructors */
		
		
		/* Accessors */
		
		/**
		 * Returns the error code.
		 * @return the error code
		 */
		public int getCode () {
			return code;
		}
		
		/* END Accessors */
		
		
		/* (Object) Methods */
		
		@Override
		public String toString () {
			return name;
		}
		
		/* END (Object) Methods */
		
		
		/* Static Methods */
		
		/**
		 * Translates an input <code>String</code> into a <code>Position</code>.
		 * @param str the input <code>String</code>
		 * @return the translated <code>Position</code>
		 */
		public static Position translate (String str) {
			return Position.valueOf(str.toUpperCase());
		}
		
		/* END Static Methods */
	}
	
	/* END (Position) Enumerator */
	
}
